<?php

class Files

{
                /*function to get the total earnings on the site*/

                function getTotalUploads($userid){
                        $query = "SELECT * FROM `uploads` WHERE `uploaded_by` = '$userid'";
                        $result = mysql_query($query);
                        $total = mysql_num_rows($result);
			return $total;
		}
		
		/*end function */
		
		
		/*function to get the users total downloads*/
		
		function getTotalDownloads($userid){
                        $query = "SELECT * FROM `downloads` WHERE `user_id` = '$userid'";
                        $result = mysql_query($query);
                        $total = mysql_num_rows($result);
			return $total;
		}
	       
	       function getUsersFileDownloads($fileid){
		          global $ado;
		          $sql = "SELECT * FROM  `downloads` WHERE `file_id` = '$fileid'"; 	          
		          $result = mysql_query($sql);
                          $total = mysql_num_rows($result);
			  return $total;
			  
	       }
	       
	      function FileExists($id){
		global $ado;	
			
		$res = $ado->exec("SELECT `file_title` FROM `uploads` WHERE `string` = '$id' LIMIT 1");
		if(mysql_num_rows($res)>0){
			return true;
		}
		else{
			return false;
		}
	        }
	        
	        function getFileName($id){
		global $ado;
		$res = $ado->exec("SELECT `file_title` FROM `uploads` WHERE `string` = '$id' LIMIT 1");
		$row = mysql_fetch_assoc($res);
		return $row['file_title'];		
	        }
	        
	        function getUploadersID($id){
		global $ado;
		$res = $ado->exec("SELECT `uploaded_by` FROM `uploads` WHERE `string` = '$id' LIMIT 1");
		$row = mysql_fetch_assoc($res);
		return $row['uploaded_by'];		
	        }
	        
	        function getUploadersUsername($id){
		global $ado;
		$res = $ado->exec("SELECT `username` FROM `users` WHERE `id` = '$id' LIMIT 1");
		$row = mysql_fetch_assoc($res);
		return $row['username'];		
	        }
	        
	        function getFileStatistics($id){
		global $ado;
		$res = $ado->exec("SELECT `statistics` FROM `uploads` WHERE `string` = '$id' LIMIT 1");
		$row = mysql_fetch_assoc($res);
		return $row['statistics'];		
	        }
	        
	        function download($filelocation){

                $filename = basename($filelocation); 
                if (file_exists($filelocation)) {
                header('Content-Description: File Transfer');
                header("Content-Type: application/force-download"); 
                header('Content-Disposition: attachment; filename='.$filename); 
                header('Content-Transfer-Encoding: binary');
                header('Expires: 0');
                header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
                header('Pragma: public');
                header('Content-Length: ' . filesize($filelocation));
                ob_clean();
                flush();
                readfile($filelocation);
                exit;
                   }
                }
                
                function getTotalVisits($fileid){
                        $query = "SELECT * FROM `file_visits` WHERE `file_id` = '$fileid'";
                        $result = mysql_query($query);
                        $total = mysql_num_rows($result);
			return $total;
		}
}

global $files;

$files = new Files();

?>